<?php
header("Content-Type: text/html; charset=utf-8");
require_once '../smarty/Smarty.class.php';
require_once '../lib/pagination.php';
require_once '../lib/query_railway.php';
require_once '../lib/base.php';

$user=$_SESSION["RU"];
//var_dump($user);
if(!$user)header("Location:../user/login.php");

if($_POST["act"]=="modify") {
	$i=$_POST["i"];
	$title=urldecode($_POST["t"]);
	$content=urldecode($_POST["c"]);
	$valid=urldecode($_POST["v"]);
	$creater=$user["nick"];
	
	$status=urldecode($_POST["s"]);
	$sremark=urldecode($_POST["e"]);
	
	if ($i){
		if($user["nick"]=="admin"){
			$_set="set title='".$title."',content='".$content."',valid='".$valid."',status='".$status."',sremark='".$sremark."' where id=".$i;
			$op=QR::update_data($tb_bl,$_set);
			if($op>0)echo "更新成功！" ;else echo "更新失败，请检查输入的正确性！";
		}else{
			$_sql="creater='".$creater."' and id='".$i."'";
			$ck=QR::check_exist($tb_bl,$_sql);
			//var_dump($ck);
			if($ck){
				$nt=strtotime(date("YmdHis"));
				$vt=strtotime($ck["valid"]);
				if($ck["status"]=="true" && $nt<$vt)exit;
				$_set="set title='".$title."',content='".$content."',valid='".$valid."',modifytime=NOW(),status='' where id=".$i;
				$op=QR::update_data($tb_bl,$_set);
				if($op>0)echo "更新成功！" ;else echo "更新失败，请检查输入的正确性！";
			}else{
				echo "数据更新失败！";
			}
		}
	}else{
		if($user["nick"]=="admin"){
			$data[0]="title,content,valid,creater,creattime,status,sremark";
			$data[1]="'$title','$content','$valid','$creater',NOW(),'$status','$sremark'";
		}else{
			$data[0]="title,content,valid,creater,creattime";
			$data[1]="'$title','$content','$valid','$creater',NOW()";		
		}
		$op= QR::add_data($tb_bl,$data);
		if($op>0)echo "添加成功！" ;else echo "添加失败，请检查输入的正确性！";
	}
	exit;
}
$id=$_GET["i"];
if($id>0){
	$str=" id='".$id."'";
	if($user["nick"]!="admin")$str.=" and creater = '".$user["nick"]."'";	
	$data=QR::get_data($tb_bl, "*",$str,0,1);
	//var_dump($data[0]);
}
//var_dump($rule);
$smarty = new Smarty;
$smarty->assign("leftpanel",leftpanel("TOOL",4));
$smarty->assign("user",$user);
$smarty->assign("rule",$data[0]);
$smarty->display('bl_set.tpl');

?>